/*
    Copyright (C) 2025 mio <stigma@disroot.org>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef ASTER_VIDEOPLAYER_H
#define ASTER_VIDEOPLAYER_H

#include <tqstring.h>
#include <tqvaluevector.h>
#include <tqwidget.h>

#include <kurl.h>

#include "mediatrack.h"

class Playlist;

struct libvlc_instance_t;
struct libvlc_media_player_t;

class VideoPlayer : public TQWidget
{
    TQ_OBJECT

public:
    VideoPlayer(TQWidget *parent = nullptr, const char *name = nullptr);
    ~VideoPlayer();

    int64_t currentTime() const;
    int64_t totalTime() const;

    bool isPaused() const;
    bool isPlaying() const;

    void showCursor();

    Playlist* playlist() const;
    void setPlaylist(Playlist*);

    int activeAudioTrack() const;
    void setActiveAudioTrack(int index);

    int activeSubtitleTrack() const;
    void setActiveSubtitleTrack(int index);

    int activeVideoTrack() const;
    void setActiveVideoTrack(int index);

    TQValueVector<MediaTrack> audioTracks() const;
    TQValueVector<MediaTrack> subtitleTracks() const;
    TQValueVector<MediaTrack> videoTracks() const;

signals:
    void lengthChanged(int);
    void stateChanged(int);
    void tick(int64_t);
    void tracksChanged();

public slots:
    void openStream(const KURL &);
    void pause();
    void play();
    void seek(int);
    void setMuted(bool muted);
    void stop();
    void setVolume(int volume);

protected:
    libvlc_instance_t *m_instance;
    libvlc_media_player_t *m_mediaPlayer;
    TQValueVector<MediaTrack> m_audioTracks;
    TQValueVector<MediaTrack> m_subtitleTracks;
    TQValueVector<MediaTrack> m_videoTracks;

    void customEvent(TQCustomEvent *) override;
    void mousePressEvent(TQMouseEvent *) override;
    void mouseMoveEvent(TQMouseEvent *e) override;
    void timerEvent(TQTimerEvent *) override;

    void setupPlayer();
    void setupTracks();

protected slots:
    void playlistItemChanged(const KURL&);

private:
    int m_mouseTimer;
    Playlist* m_playlist;
};

#endif
